'use strict';

var remoteVideo = document.getElementById("remoteVideo");
var pullBtn = document.getElementById("pullBtn");
var stopPullBtn = document.getElementById("stopPullBtn");

pullBtn.addEventListener("click", startPull);
stopPullBtn.addEventListener("click", stopPull);

var uid = $("#uid").val();
var streamName = $("#streamName").val();
var audio = $("#audio").val();
var video = $("#video").val();
var mode = $("#mode").val();

var offer = "";
var pc;
const config = {};
var remoteStream;
var lastConnectStatus = "";

function startPull() {
    console.log("send push: /signaling/pull");

    $.post("/signaling/pull",
        { "uid": uid, "streamName": streamName, "audio": audio, "video": video,"isDtls": 1,"mode":mode },
        function (data, textStatus) {
            console.log("push response: " + JSON.stringify(data));
            if ("success" == textStatus && 0 == data.errNo) {
                $("#tips1").html("<font color='blue'>拉流请求成功!</font>");
                console.log("offer sdp: \n" + data.data.sdp);
                offer = data.data;
                pullStream();
            } else {
                $("#tips1").html("<font color='red'>拉流请求失败!</font>");
            }
        },
        "json"
    );
}
function stopPull() {

    console.log("send stop pull:/signaling/stoppull");
    remoteVideo.srcObject = null;
    if (remoteStream && remoteStream.getAudioTracks()) {
        remoteStream.getAudioTracks()[0].stop();
    }
    if (remoteStream && remoteStream.getVideoTracks()) {
        remoteStream.getVideoTracks()[0].stop();
    }
    if (pc) {
        pc.close()
        pc = null;
    }

    $("#tips1").html("");
    $("#tips2").html("");
    $("#tips3").html("");

    $.post("/signaling/stoppull",
        { "uid": uid, "streamName": streamName ,"mode":mode},
        function (data, textStatus) {
            console.log("stoppull response: " + JSON.stringify(data));
            if ("success" == textStatus && 0 == data.errNo) {
                $("#tips1").html("<font color='blue'>停止拉流请求成功!</font>");
            } else {
                $("#tips1").html("<font color='red'>停止拉流请求失败!</font>");
            }
        },
        "json"
    );


}
function sendAnswer(answerSdp) {
    console.log("send answer: /signaling/sendanswer");

    $.post("/signaling/sendanswer",
        { "uid": uid, "streamName": streamName, "answer": answerSdp,
             "type": "pull","mode":mode },
        function (data, textStatus) {
            console.log(" send answer response:" + JSON.stringify(data));
            if ("success" == textStatus && 0 == data.errNo) {
                $("#tips3").html("<font color='blue'>Answer发送成功!</font>");
            } else {
                $("#tips3").html("<font color='red'>Answer发送失败!</font>");
            }
        },
        "json"
    );


}

function pullStream() {
    pc = new RTCPeerConnection(config);
    pc.onconnectionstatechange = function (e) {
        var state = "";
        if (lastConnectStatus != "") {
            state = lastConnectStatus + "->" + pc.iceConnectionState;
        } else {
            state = pc.iceConnectionState;
        }
        $("#tips2").html("链接状态:" + state);
        lastConnectStatus = pc.iceConnectionState;

    };
    pc.onaddstream = function (e) {

        remoteStream = e.stream;
        remoteVideo.srcObject = e.stream;
    }
    console.log("set remote sdp start");
    pc.setRemoteDescription(offer).then(
        setRemoteDescriptionSuccess,
        setRemoteDescriptionError
    );
}


function setRemoteDescriptionSuccess() {
    console.log("pc set remote description success!!!!!");
    pc.createAnswer().then(
        createSessionDescriptionSuccess,
        createSessionDescriptionError
    );
}

function createSessionDescriptionSuccess(answer) {

    console.log("answer sdp:\n" + answer.sdp);
    console.log("pc set local sdp");
    pc.setLocalDescription(answer).then(
        setLocalDescriptionSuccess,
        setLocalDescriptionError
    );
    //发送answer 给服务器----
    sendAnswer(answer.sdp);
}

function setRemoteDescriptionError(error) {

    console.log("pc set remote sdp error:" + error);
}
function createSessionDescriptionError(error) {

    console.log("pc create answer error\n" + error);


}

function setLocalDescriptionSuccess() {

    console.log("pc set local sdp success");
}


function setLocalDescriptionError(error) {
    console.log("pc set local sdp error : " + error);
}

